﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.Data.Entity;

namespace DNA.Mvc.Management
{
    public class EntitySpammerRepository : EntityRepositoryBase<LogDB, Spammer>, ISpammerRepository
    {
        public EntitySpammerRepository(string nameOrConnectionString)
        {
            context = new LogDB(nameOrConnectionString);
            dbSet = this.Context.Set<Spammer>();
            IsOwnContext = true;
        }

        public EntitySpammerRepository(LogDB dbContext)
        {
            context = dbContext;
            dbSet = this.Context.Set<Spammer>();
            IsOwnContext = false;
        }

        public bool IsSpam(string ip, string url)
        {
            if (string.IsNullOrEmpty(ip) && string.IsNullOrEmpty(url))
                throw new ArgumentNullException("ip");

            if (!string.IsNullOrEmpty(url))
                return DbSet.Count(s => s.IP.Equals(ip) || s.Url.Equals(url)) > 0;
            else
                return DbSet.Count(s => s.IP.Equals(ip)) > 0;
        }

        public bool IsSpam(string username)
        {
            if (string.IsNullOrEmpty(username))
                throw new ArgumentNullException("username");

            return DbSet.Count(s => s.UserName.Equals(username)) > 0;
        }
    }
}
